﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace XSHTToPdf
{
    public partial class Main : Form
    {
        public Main(string[] args)
        {
            InitializeComponent();
            putin = args;
        }
        PDF pdf = new PDF();
        PDFPlus pdfplus = new PDFPlus();
        MsSqlHelper mssql = new MsSqlHelper();
        public string EnterpriseName = ConfigurationManager.AppSettings["EnterpriseName"].ToString();
        public string[] putin = null;

        private void Main_Load(object sender, EventArgs e)
        {
            DateTime StartDate = Convert.ToDateTime("2020-10-11");
            TimeSpan span = StartDate.Subtract(DateTime.Now);
            if (putin.Length != 0)
            {
                ToPdf(putin[0].Trim());
                this.Close();
                Application.Exit();
            }
        }

        public void ToPdf(string Number)
        {
            string sql = "";
            sql = "select c.spmch,c.shpgg,c.dw,b.shl,b.dj,b.hsje,left(c.shengccj,45) as shengccj "
                +"from [pf_djmx] b(nolock),[spkfk] c(nolock)" 
                +" where  b.spid=c.spid and b.djbh='"+Number+"'";            
            DataTable dt = mssql.ExecuteDt(sql);
            sql = "select "
                +"rtrim(a.djbh)+','+rtrim(a.rq)+','+rtrim(b.dwmch)+','+cast(a.hsje as varchar(20))+','"
                + "+rtrim(a.thfs)+','+rtrim(a.yunfcd)+','+rtrim(a.fkfs)+','+rtrim(a.faplx)+','"
				+"+rtrim(a.username) +','"
				+"+rtrim(dzhdh)+','+rtrim(khyh)+','+rtrim(yhzhh)+','+rtrim(shh)"
                +" from [pf_djhz] a(nolock),[mchk] b(nolock) "
                +" where a.dwbh=b.dwbh and djbh='" + Number + "'";
            string header = mssql.GetString(sql);
            string[] headers = header.Split(',');
            
            string saveFileName = "";
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "pdf";
            saveDialog.Filter = "PDF文件|*.pdf";
            saveDialog.FileName = Number;

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                saveFileName = saveDialog.FileName;

               // if (pdf.CreatePDF(saveFileName,headers, dt))
               if (pdfplus.CreatePDF(saveFileName, headers, dt))
                {
                    FormMSG msg = new FormMSG(Number);
                    msg.ShowDialog();
                }
                else
                {
                    MessageBox.Show("报错了！");
                }
            }
        }

        private void btnPDF_Click(object sender, EventArgs e)
        {
            ToPdf(txtdjbh.Text.Trim());
        }
    }
}
